﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SES.CMS.BL;
using SES.CMS.DO;

namespace SES.CMS.AdminCP.PageUC
{
    public partial class ucProduct : System.Web.UI.UserControl
    {
        cmsSanPhamDO objSanPham = new cmsSanPhamDO();

        protected void Page_Load(object sender, EventArgs e)
        {
            Functions.ddlDatabinder(drpLoaiSanPham, cmsLoaiSanPhamDO.LOAISANPHAMID_FIELD, cmsLoaiSanPhamDO.TENLOAISANPHAM_FIELD, new cmsLoaiSanPhamBL().SelectAll(0));
            Functions.ddlDatabinder(drpNhanHieu,cmsNhanHieuDO.NHANHIEUID_FIELD,cmsNhanHieuDO.TENNHANHIEU_FIELD,new cmsNhanHieuBL().SelectAll());
            Functions.ddlDatabinder(drpDonViTinh, cmsDonViDO.DONVIID_FIELD, cmsDonViDO.DONVITINH_FIELD, new cmsDonViBL().SelectAll());
            Functions.ddlDatabinder(ddlCollection, cmsCollectionDO.COLLECTIONID_FIELD, cmsCollectionDO.TITLE_FIELD, new cmsCollectionBL().SelectAll());

            if (Request.QueryString["ProductID"] != null)
            {
                objSanPham.SanPhamID = int.Parse(Request.QueryString["ProductID"].ToString());
                InitForm();
            }
        }
// Save_Button
        long i;
        protected void btSave_Click(object sender, EventArgs e)
        {
            SetObject();
            if (objSanPham.SanPhamID <= 0)
            {
                i = new cmsSanPhamBL().Insert(objSanPham);
                Functions.Alert("Thêm mới thành công", "Default.aspx?Page=ListProduct");
            }
            else
            {
                i = new cmsSanPhamBL().Update(objSanPham);
                i = objSanPham.SanPhamID;
                Functions.Alert("Cập nhật thành công", "Default.aspx?Page=ListProduct");
            }

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx?Page=ListProduct");
        }
// InitForm();
        protected void InitForm()
        {
            objSanPham = new cmsSanPhamBL().Select(objSanPham);
            if (objSanPham.Mota == null)
                txtMota.Text = "Hay them mo ta";
            else
                txtMota.Text = objSanPham.Mota;

            txtTenSanPham.Text = objSanPham.TenSanPham;
            txtProductName.Text = objSanPham.ProductName;
            txtMaSanPham.Text = objSanPham.MaSanPham;
            txtGia.Text = objSanPham.DonGia.ToString();
            txtPrice.Text = objSanPham.Price.ToString();
            txtGiamGia.Text = objSanPham.GiamGia.ToString();
            cbSanPhamBanChay.Checked = objSanPham.SanPhamBanChay;
            cbSanPhamMoi.Checked = objSanPham.SanPhamMoi;
            if (!string.IsNullOrEmpty(objSanPham.HinhAnh))
            {
                hplImage.Visible = true;
                hplImage.NavigateUrl = "~/Media/" + objSanPham.HinhAnh;
            }

            txtMoTaChiTiet.Text = objSanPham.MoTaChiTiet;
            txtTuKhoa.Text = objSanPham.TuKhoa;
            txtBaoHanh.Text = objSanPham.BaoHanh;
            txtDescription.Text = objSanPham.Description;
            txtDetailDescription.Text = objSanPham.DetailDescription;
            txtWarranty.Text = objSanPham.Warranty;

            try
            {
                drpLoaiSanPham.SelectedValue = objSanPham.LoaiSanPhamID.ToString();
            }
            catch{}

            try
            {
                drpNhanHieu.SelectedValue = objSanPham.NhanHieuID.ToString();
            }catch{}
            try
            {
                drpDonViTinh.SelectedValue = objSanPham.DonViID.ToString();
            }
            catch { }
            try
            {
                ddlCollection.SelectedValue = objSanPham.CollectionID.ToString();
            }
            catch { }
        }
// SetObject()
        protected void SetObject()
        {
            objSanPham.TenSanPham = txtTenSanPham.Text;
            objSanPham.ProductName = txtProductName.Text;
            objSanPham.MaSanPham = txtMaSanPham.Text;
            objSanPham.NhanHieuID = int.Parse(drpNhanHieu.SelectedValue);
            objSanPham.LoaiSanPhamID = int.Parse(drpLoaiSanPham.SelectedValue);
            objSanPham.DonViID = int.Parse(drpDonViTinh.SelectedValue);
            objSanPham.CollectionID = int.Parse(ddlCollection.SelectedValue);
            
            if (!string.IsNullOrEmpty(fulImage.FileName))
            {
                string FileName = string.Format("{0}{1}",fulImage.FileName.Substring(0,fulImage.FileName.LastIndexOf(".")) + "-" + DateTime.Now.ToString("dd-MM-yyyy-hhmmss"),fulImage.FileName.Substring(fulImage.FileName.LastIndexOf(".")));
                string SaveLocation = string.Format("{0}\\{1}",Server.MapPath("~/Media/"),FileName);
                fulImage.SaveAs(SaveLocation);
                objSanPham.HinhAnh = FileName;
            }
            if (!string.IsNullOrEmpty(txtGia.Text))
            {
                objSanPham.DonGia = int.Parse(txtGia.Text);
            }
            if (!string.IsNullOrEmpty(txtGiamGia.Text))
            {
                objSanPham.GiamGia = int.Parse(txtGiamGia.Text);
            }

            objSanPham.MoTaChiTiet = txtMoTaChiTiet.Text;
            objSanPham.TuKhoa = txtTuKhoa.Text;
            objSanPham.BaoHanh = txtBaoHanh.Text;
            objSanPham.SanPhamBanChay = cbSanPhamBanChay.Checked;
            objSanPham.SanPhamMoi = cbSanPhamMoi.Checked;
            objSanPham.Mota = txtMota.Text;
            objSanPham.Warranty = txtWarranty.Text;
            objSanPham.Description = txtDescription.Text;
            objSanPham.DetailDescription = txtDetailDescription.Text;

        }

  
    }
}